<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card class="box-card">
        <el-row :gutter="20">
            <el-col :span="7">
                <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query" @clear="getuserList">
                    <el-button slot="append" icon="el-icon-search" @click="getuserList" clearable></el-button>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="setAddDialogVisible('新增用户')">添加用户</el-button>
            </el-col>
        </el-row>
    </el-card>

    <!-- 用户展现区域 -->
    <el-table :data="userList" border stripe>
        <el-table-column type="index" lable='#'></el-table-column>
        <el-table-column label='姓名' prop="username"></el-table-column>
        <el-table-column label='邮箱' prop="email"></el-table-column>
        <el-table-column label='电话' prop="mobile"></el-table-column>
        <el-table-column label='角色' prop="role_name"></el-table-column>
        <el-table-column label='状态' prop="mg_state">
            <template v-slot:default="scope">
                <el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)"></el-switch>
            </template>
        </el-table-column>
        <el-table-column label='操作' width='180px'>
            <template  v-slot:default="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="setAddDialogVisible( '修改用户' , scope.row.id)"></el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)"></el-button>
                <el-tooltip  effect="dark" content="分配角色" placement="top" :enterable="false">
                      <el-button type="warning" icon="el-icon-setting" size="mini" @click="setAddDialogVisible('分配角色','',scope.row)"></el-button>
                </el-tooltip>
            </template>
        </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

  <!-- 添加用户修改用户组件 -->
    <SetDialog
              v-if="addDialogVisible"
              @change = setAddDialogVisible
              :title = title
              :id = id
              :getuserList = getuserList
              :userinfo1 = userinfo
              :rolesListinfo= rolesListinfo>
    </SetDialog>
  </div>
</template>

<script>
import SetDialog from '@/components/user/setDialog'
import { instance } from '@/utils/http'
export default {
  components: {
    SetDialog
  },
  created () {
    this.getuserList()
  },
  data () {
    return {
      rolesListinfo: [],

      queryInfo: {
        query: '',
        pagenum: 1,
        // 获取几条数据
        pagesize: 2
      },
      userList: [],
      total: 0,

      // 用户对话框的显示与隐藏
      addDialogVisible: false,
      // 传入子组件的信息
      title: '',
      id: '',
      userinfo: {}
    }
  },
  methods: {
    async rolesList () {
      const { data: res } = await instance.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('出错了')
      }
      this.rolesListinfo = res.data
    },
    // 修改自身addDialogVisible的方法
    setAddDialogVisible (title, id, scope) {
      this.addDialogVisible = !this.addDialogVisible
      this.title = title
      this.id = id
      this.userinfo = scope
      this.rolesList()
      // console.log(title, id)
    },
    // 封装获取用户信息的方法
    async getuserList () {
      const { data: res } = await instance.get('/users', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.console.error(res.meta.msg)
      this.userList = res.data.users
      this.total = res.data.total
      // console.log(res)
    },
    handleSizeChange (newSize) {
      this.$nextTick(() => {
        this.queryInfo.pagesize = newSize
        // console.log(newSize, this.queryInfo.pagesize)
        this.getuserList()
      })
    },
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.$nextTick(() => {
        this.queryInfo.pagenum = newPage
        this.getuserList()
      })
    },
    async userStateChanged (userInfo) {
      // console.log(userInfo)
      const { data: res } = await instance.put('users/' + userInfo.id + '/state/' + userInfo.mg_state)
      if (res.meta.status !== 200) {
        userInfo.mg_state = !userInfo.mg_state
        return this.$message.error('更新用户状态失败')
      }
      this.$message.success('更新用户状态成功')
    },

    async removeUserById (id) {
      // 根据Id值删除
      const comfirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(comfirmResult)
      if (comfirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      } else {
        const { data: res } = await instance.delete('users/' + id)
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('删除成功')
        this.getuserList()
      }
    }
  }
}
</script>

<style scoped lang="less">

</style>
